<template>
  <div
    class="loading"
    :class="loadingClass"
    :style="loadingStyle"
    v-bind="$attrs"
  >
    <!-- 加载指示器 -->
    <div class="loading-indicator" :class="indicatorClass">
      <!-- 旋转器类型 -->
      <div v-if="type === 'spinner'" class="spinner">
        <div class="spinner-circle"></div>
      </div>

      <!-- 点点类型 -->
      <div v-else-if="type === 'dots'" class="dots">
        <div class="dot" v-for="i in 3" :key="i"></div>
      </div>

      <!-- 脉冲类型 -->
      <div v-else-if="type === 'pulse'" class="pulse">
        <div class="pulse-circle"></div>
      </div>

      <!-- 波浪类型 -->
      <div v-else-if="type === 'wave'" class="wave">
        <div class="wave-bar" v-for="i in 5" :key="i"></div>
      </div>

      <!-- 环形类型 -->
      <div v-else-if="type === 'ring'" class="ring">
        <div class="ring-circle"></div>
      </div>

      <!-- 进度条类型 -->
      <div v-else-if="type === 'progress'" class="progress">
        <div class="progress-bar" :style="{ width: `${progress}%` }"></div>
      </div>

      <!-- 自定义图标 -->
      <Icon
        v-else-if="type === 'icon' && icon"
        :name="icon"
        :size="iconSize"
        class="loading-icon"
      />

      <!-- 自定义内容 -->
      <div v-else-if="type === 'custom'" class="custom-content">
        <slot name="indicator"></slot>
      </div>
    </div>

    <!-- 加载文本 -->
    <div v-if="text || $slots.default" class="loading-text">
      <slot>{{ text }}</slot>
    </div>

    <!-- 描述文本 -->
    <div v-if="description" class="loading-description">
      {{ description }}
    </div>

    <!-- 遮罩层（用于覆盖模式） -->
    <div
      v-if="overlay"
      class="loading-overlay"
      @click="handleOverlayClick"
    ></div>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import Icon from "./Icon.vue";

interface Props {
  // 基础属性
  type?:
    | "spinner"
    | "dots"
    | "pulse"
    | "wave"
    | "ring"
    | "progress"
    | "icon"
    | "custom";
  size?: "mini" | "small" | "medium" | "large" | "huge";
  color?: string;

  // 文本
  text?: string;
  description?: string;

  // 图标模式
  icon?: string;

  // 进度模式
  progress?: number;

  // 布局
  vertical?: boolean;
  centered?: boolean;
  fullscreen?: boolean;
  overlay?: boolean;

  // 样式
  background?: string;
  blur?: boolean;

  // 其他
  spinning?: boolean;
  delay?: number;
}

const props = withDefaults(defineProps<Props>(), {
  type: "spinner",
  size: "medium",
  vertical: true,
  centered: false,
  fullscreen: false,
  overlay: false,
  blur: false,
  spinning: true,
  progress: 0,
  delay: 0,
});

const emit = defineEmits<{
  overlayClick: [];
}>();

// 计算属性
const loadingClass = computed(() => {
  return [
    `loading-${props.size}`,
    `loading-${props.type}`,
    {
      "loading-vertical": props.vertical,
      "loading-horizontal": !props.vertical,
      "loading-centered": props.centered,
      "loading-fullscreen": props.fullscreen,
      "loading-overlay": props.overlay,
      "loading-blur": props.blur,
      "loading-spinning": props.spinning,
    },
  ];
});

const indicatorClass = computed(() => {
  return [
    `indicator-${props.size}`,
    {
      "indicator-colored": props.color,
    },
  ];
});

const loadingStyle = computed(() => {
  const styles: Record<string, string> = {};

  if (props.color) {
    styles["--loading-color"] = props.color;
  }

  if (props.background) {
    styles["--loading-background"] = props.background;
  }

  if (props.delay > 0) {
    styles.animationDelay = `${props.delay}ms`;
  }

  return styles;
});

const iconSize = computed(() => {
  const sizeMap = {
    mini: 16,
    small: 20,
    medium: 24,
    large: 32,
    huge: 48,
  };
  return sizeMap[props.size];
});

// 事件处理
const handleOverlayClick = () => {
  emit("overlayClick");
};
</script>

<style scoped>
/* 基础样式 */
.loading {
  --loading-color: var(--primary-color);
  --loading-background: rgba(255, 255, 255, 0.9);
  --loading-text-color: var(--text-primary);
  --loading-description-color: var(--text-secondary);

  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  position: relative;
}

/* 布局方向 */
.loading-vertical {
  flex-direction: column;
  gap: 8px;
}

.loading-horizontal {
  flex-direction: row;
  gap: 12px;
}

/* 居中 */
.loading-centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* 全屏 */
.loading-fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  background-color: var(--loading-background);
}

/* 遮罩层 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--loading-background);
  z-index: -1;
}

.loading-blur .loading-overlay {
  backdrop-filter: blur(4px);
}

/* 加载指示器 */
.loading-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--loading-color);
}

/* 尺寸 */
.indicator-mini {
  width: 16px;
  height: 16px;
}

.indicator-small {
  width: 20px;
  height: 20px;
}

.indicator-medium {
  width: 24px;
  height: 24px;
}

.indicator-large {
  width: 32px;
  height: 32px;
}

.indicator-huge {
  width: 48px;
  height: 48px;
}

/* 旋转器 */
.spinner {
  width: 100%;
  height: 100%;
  position: relative;
}

.spinner-circle {
  width: 100%;
  height: 100%;
  border: 2px solid transparent;
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 点点 */
.dots {
  display: flex;
  gap: 4px;
  align-items: center;
}

.dot {
  width: 6px;
  height: 6px;
  background-color: currentColor;
  border-radius: 50%;
  animation: dot-bounce 1.4s ease-in-out infinite both;
}

.dot:nth-child(1) {
  animation-delay: -0.32s;
}
.dot:nth-child(2) {
  animation-delay: -0.16s;
}
.dot:nth-child(3) {
  animation-delay: 0s;
}

@keyframes dot-bounce {
  0%,
  80%,
  100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 脉冲 */
.pulse {
  width: 100%;
  height: 100%;
  position: relative;
}

.pulse-circle {
  width: 100%;
  height: 100%;
  background-color: currentColor;
  border-radius: 50%;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0% {
    transform: scale(0);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

/* 波浪 */
.wave {
  display: flex;
  gap: 2px;
  align-items: center;
  height: 100%;
}

.wave-bar {
  width: 3px;
  height: 100%;
  background-color: currentColor;
  border-radius: 1.5px;
  animation: wave-bounce 1.2s ease-in-out infinite;
}

.wave-bar:nth-child(1) {
  animation-delay: -1.1s;
}
.wave-bar:nth-child(2) {
  animation-delay: -1s;
}
.wave-bar:nth-child(3) {
  animation-delay: -0.9s;
}
.wave-bar:nth-child(4) {
  animation-delay: -0.8s;
}
.wave-bar:nth-child(5) {
  animation-delay: -0.7s;
}

@keyframes wave-bounce {
  0%,
  40%,
  100% {
    transform: scaleY(0.4);
  }
  20% {
    transform: scaleY(1);
  }
}

/* 环形 */
.ring {
  width: 100%;
  height: 100%;
  position: relative;
}

.ring-circle {
  width: 100%;
  height: 100%;
  border: 2px solid rgba(0, 0, 0, 0.1);
  border-left: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

/* 进度条 */
.progress {
  width: 100px;
  height: 4px;
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background-color: currentColor;
  border-radius: 2px;
  transition: width 0.3s ease;
}

/* 图标 */
.loading-icon {
  animation: spin 2s linear infinite;
}

/* 自定义内容 */
.custom-content {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 文本 */
.loading-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--loading-text-color);
  text-align: center;
  white-space: nowrap;
}

.loading-description {
  font-size: 12px;
  color: var(--loading-description-color);
  text-align: center;
  margin-top: 4px;
}

/* 尺寸变体 */
.loading-mini .loading-text {
  font-size: 12px;
}

.loading-small .loading-text {
  font-size: 13px;
}

.loading-large .loading-text {
  font-size: 16px;
}

.loading-huge .loading-text {
  font-size: 18px;
  font-weight: 600;
}

.loading-mini .loading-description {
  font-size: 11px;
}

.loading-small .loading-description {
  font-size: 11px;
}

.loading-large .loading-description {
  font-size: 13px;
}

.loading-huge .loading-description {
  font-size: 14px;
}

/* 动画控制 */
.loading:not(.loading-spinning) .loading-indicator {
  animation-play-state: paused;
}

.loading:not(.loading-spinning) .spinner-circle,
.loading:not(.loading-spinning) .ring-circle,
.loading:not(.loading-spinning) .loading-icon {
  animation-play-state: paused;
}

.loading:not(.loading-spinning) .dot,
.loading:not(.loading-spinning) .pulse-circle,
.loading:not(.loading-spinning) .wave-bar {
  animation-play-state: paused;
}

/* 响应式 */
@media (max-width: 768px) {
  .loading-horizontal {
    flex-direction: column;
    gap: 8px;
  }

  .loading-text {
    font-size: 13px;
  }

  .loading-description {
    font-size: 11px;
  }
}

/* 暗色主题适配 */
@media (prefers-color-scheme: dark) {
  .loading {
    --loading-background: rgba(0, 0, 0, 0.8);
  }

  .progress {
    background-color: rgba(255, 255, 255, 0.1);
  }

  .ring-circle {
    border-color: rgba(255, 255, 255, 0.1);
    border-left-color: currentColor;
  }
}

/* 无障碍支持 */
@media (prefers-reduced-motion: reduce) {
  .spinner-circle,
  .ring-circle,
  .loading-icon {
    animation: none;
  }

  .dot,
  .pulse-circle,
  .wave-bar {
    animation: none;
  }

  .progress-bar {
    transition: none;
  }
}

/* 高对比度模式 */
@media (prefers-contrast: high) {
  .loading {
    --loading-color: var(--text-primary);
  }

  .progress {
    background-color: var(--border-color);
  }
}
</style>
